package valid_contr
{
	import flash.events.*;
	import flash.geom.*;
	
	import mx.controls.*;
	import mx.events.*;
	import mx.managers.*;
	import mx.validators.*;

	public class decimal_textinput extends TextInput
	{
		private var validatorobj:NumberValidator = new NumberValidator
		private var errorTip:ToolTip
		private var tip_position:String
		
		public function decimal_textinput()
		{
			super();
			validatorobj.source = this;
			validatorobj.listener = undefined;
			validatorobj.property = "text";
			validatorobj.domain = "real";
			this.addEventListener(MouseEvent.CLICK, clearvalidation);
			this.addEventListener(FocusEvent.FOCUS_OUT, validate2);
		}
		
		private function validate2(e:Event):void{
			validate()
		}
		
		public function validate():Boolean
		{
			if (validatorobj.validate(null,true).type == ValidationResultEvent.INVALID){
			
			try{
				ToolTipManager.destroyToolTip(errorTip);
			}
			catch(ex:Object){}
				var pt:Point = new Point(0, 0);
        		pt = this.localToGlobal(pt);
				this.errorString = validatorobj.validate(null,true).message
				switch(tipposition){
					case "right":
							errorTip = ToolTipManager.createToolTip(validatorobj.validate(null,true).message,pt.x + this.width + 5,pt.y,"errorTipRight") as ToolTip;
							break;
					case "top":
							errorTip = ToolTipManager.createToolTip(validatorobj.validate(null,true).message,pt.x+5,pt.y - 45,"errorTipAbove") as ToolTip;
							break;
					case "bottom":
							errorTip = ToolTipManager.createToolTip(validatorobj.validate(null,true).message,pt.x+5,pt.y + this.height + 5,"errorTipBelow") as ToolTip;
							break;
					default:
							errorTip = ToolTipManager.createToolTip(validatorobj.validate(null,true).message,pt.x + this.width + 5,pt.y) as ToolTip;
							break;
				}
				errorTip.setStyle("styleName", "errorTip");
				return false
			}
			else
			{
				try{
				ToolTipManager.destroyToolTip(errorTip);
			}
			catch(ex:Object){}
			}
			return true
		}
		
		public function clearvalidation(e:Event):void{
			this.errorString = '';
			try{
				ToolTipManager.destroyToolTip(errorTip);
			}
			catch(ex:Object){}
		}
		
		public function get required():Boolean
		{
			return validatorobj.required;
		}

		public function set required(bool:Boolean):void
		{
			validatorobj.required = bool;
		}
		
		public function get minValue():String
		{
			return validatorobj.minValue.toString();
		}

		public function set minValue(length:String):void
		{
			validatorobj.minValue = length;
		}
		
		public function get maxValue():String
		{
			return validatorobj.maxValue.toString();
		}

		public function set maxValue(length:String):void
		{
			validatorobj.maxValue = length;
		}
		
		public function get precision():String
		{
			return validatorobj.precision.toString();
		}

		public function set precision(precision:String):void
		{
			validatorobj.precision = precision;
		}
		public function get tipposition():String
		{
			return tip_position;
		}

		public function set tipposition(tipposition2:String):void
		{
			tip_position = tipposition2;
		}		
		
		public function get cleantext():String{
			
			return this.text.replace(',','');
			
		}
		
		public function set cleantext(str:String):void{
			
			this.text = cleantext;
			
		}
		
	}
}